const MyPromise = require("../template.js")

/** 十五、Promise.reject 静态方法测试 */

// 测试 1: 基本 reject
function testRejectBasic() {
  MyPromise.reject("Error").then(
    (value) => console.log("15.1 Reject basic:", false),
    (reason) => console.log("15.1 Reject basic:", reason === "Error")
  )
}

// 测试 2: reject Error 对象
function testRejectError() {
  const error = new Error("Test Error")
  MyPromise.reject(error).then(
    (value) => console.log("15.2 Reject Error:", false),
    (reason) => console.log("15.2 Reject Error:", reason.message === "Test Error")
  )
}

// 测试 3: reject 数字
function testRejectNumber() {
  MyPromise.reject(404).then(
    (value) => console.log("15.3 Reject number:", false),
    (reason) => console.log("15.3 Reject number:", reason === 404)
  )
}

// 测试 4: reject 对象
function testRejectObject() {
  const obj = { code: 500, message: "Server Error" }
  MyPromise.reject(obj).then(
    (value) => console.log("15.4 Reject object:", false),
    (reason) => console.log("15.4 Reject object:", reason.code === 500 && reason.message === "Server Error")
  )
}

// 测试 5: reject 链式调用
function testRejectChain() {
  MyPromise.reject("Initial Error")
    .catch((reason) => {
      console.log("15.5 Reject chain - catch:", reason === "Initial Error")
      return "Recovered"
    })
    .then((value) => console.log("15.6 Reject chain - recovered:", value === "Recovered"))
}

// 测试 6: reject 不会解包 Promise（与 resolve 不同）
function testRejectPromise() {
  const innerPromise = MyPromise.resolve("Inner")
  MyPromise.reject(innerPromise).then(
    (value) => console.log("15.7 Reject Promise:", false),
    (reason) => console.log("15.7 Reject Promise:", reason instanceof MyPromise)
  )
}

// 测试 7: reject undefined
function testRejectUndefined() {
  MyPromise.reject(undefined).then(
    (value) => console.log("15.8 Reject undefined:", false),
    (reason) => console.log("15.8 Reject undefined:", reason === undefined)
  )
}

// 测试 8: reject null
function testRejectNull() {
  MyPromise.reject(null).then(
    (value) => console.log("15.9 Reject null:", false),
    (reason) => console.log("15.9 Reject null:", reason === null)
  )
}

testRejectBasic()
testRejectError()
testRejectNumber()
testRejectObject()
testRejectChain()
testRejectPromise()
testRejectUndefined()
testRejectNull()

